<template>
  <div>
    <h2>用户管理</h2>
    <div>
      <el-form
        :inline="true"
        :model="queryParams"
        class="demo-form-inline"
        size="small"
      >
        <span>
          <el-form-item>
            <el-select
              v-model="queryParams.searchType"
              placeholder="搜索类型"
              style="width: 100px"
            >
              <el-option label="用户名" value="用户名"></el-option>
              <el-option label="证件号" value="证件号"></el-option>
            </el-select>
          </el-form-item>
        </span>
        <span>
          <el-form-item>
            <el-input
              v-model="queryParams.content"
              placeholder="请输入要查询的用户名称或证件号"
              style="width: 250px"
            ></el-input>
          </el-form-item>
        </span>
        <el-form-item>
          <el-button type="primary" @click="query()">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="add()"><i class="el-icon-plus"></i>新增</el-button>
        </el-form-item>
      </el-form>

      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-row>
            <el-col :span="10">
              <el-form-item label="用户名" :label-width="formLabelWidth">
                <el-input v-model="form.username" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="证件类型" :label-width="formLabelWidth">
                <el-select v-model="form.region" placeholder="请选择">
                  <el-option label="身份证" value="身份证"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="证件号" :label-width="formLabelWidth">
                <el-input v-model="form.idcard" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="所属机构" :label-width="formLabelWidth">
                <el-input v-model="form.dep" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="联系方式" :label-width="formLabelWidth">
                <el-input v-model="form.tel" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="用户角色" :label-width="formLabelWidth">
                <el-select v-model="form.role.rolename" placeholder="请选择">
                  <el-option
                    v-for="item in roles"
                    :key="item.id"
                    :label="item.rolename"
                    :value="item.rolename"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="上级" :label-width="formLabelWidth">
                <el-input
                  v-model="form.leader.username"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="commit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-table
        :data="userData"
        style="width: 100%"
        :header-cell-style="{ background: '#f2f2f2' }"
        size="mini"
      >
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="120"
        ></el-table-column>
        <el-table-column label="证件类型" width="120">
          <template>
            <span>身份证</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="idcard"
          label="证件号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="dep"
          label="所属机构"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="tel"
          label="联系方式"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="role.rolename"
          label="用户角色"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="leader"
          label="上级用户"
          width="140"
        ></el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              ><i class="el-icon-edit"></i>编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              ><i class="el-icon-delete"></i>删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  created() {
    this.getUsers();
  },
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      //初始显示
      pageIndex: 1,
      pageSize: 10,
      queryParams: {
        content: "",
        searchType: "用户名",
      },
      roles: [],
      pagebeanData: {
        beanList: [],
        totalRecord: 0,
      },
      userData: [],
      dialogFormVisible: false,
      title: "添加用户",
      form: {
        id: "",
        username: "",
        region: "",
        idcard: "",
        dep: "",
        tel: "",
        role: {
          rolename: "",
        },
        leader: {
          username: "",
        },
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    query() {
      console.log(this.queryParams.searchType);
      if (this.queryParams.searchType == "证件号") {
        this.$axios
          .get("http://localhost:8800/user/queryByidcard", {
            params: {
              idcard: this.queryParams.content,
            },
          })
          .then((resp) => {
            this.userData = resp.data.data;
          });
      } else if (this.queryParams.searchType == "用户名") {
        this.$axios
          .get("http://localhost:8800/user/queryByname", {
            params: {
              username: this.queryParams.content,
            },
          })
          .then((resp) => {
            this.userData = resp.data.data;
          });
      }
    },
    getUsers() {
      this.$axios
        .get("http://localhost:8800/user/all", this.user)
        .then((resp) => {
          this.userData = resp.data;
        });
    },
    reset() {
      console.log("重置!");
      this.queryParams.searchType = "用户名";
      this.queryParams.content = "";
    },
    add() {
      this.title = "添加用户";
      this.form.username = "";
      this.form.idcard = "";
      this.form.tel = "";
      this.form.dep = "";
      this.form.role.rolename = "";
      this.form.region = "";
      this.$axios.get("http://localhost:8800/role/all").then((resp) => {
        console.log(resp.data);
        this.roles = resp.data;
      });
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAttendance();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getAttendance();
      this.pageIndex = 1;
    },
    handleEdit(index, row) {
      this.title = "修改用户";
      this.form.id = row.id;
      this.$axios
        .get("http://localhost:8800/user/get", {
          params: {
            id: row.id,
          },
        })
        .then((resp) => {
          console.log(resp.data);
          this.form.username = resp.data.username;
          this.form.idcard = resp.data.idcard;
          this.form.tel = resp.data.tel;
          this.form.dep = resp.data.dep;
          this.form.role.rolename = resp.data.role.rolename;
          this.form.region = "身份证";
          this.dialogFormVisible = true;
        });
      this.$axios.get("http://localhost:8800/role/all").then((resp) => {
        console.log(resp.data);
        this.roles = resp.data;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("确认删除改用户吗？", "删除用户", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确认",
        cancelButtonText: "取消",
      })
        .then(() => {
          this.$axios
            .delete("http://localhost:8800/user/del/" + row.id)
            .then((resp) => {
              console.log(resp.data);
              if (resp.data.code == 200) {
                this.$message({
                  message: "用户删除成功！",
                  type: "success",
                });
              } else {
                this.$message.error("用户删除失败！");
              }
              this.getUsers();
            });
        })
        .catch((action) => {
          this.$message({
            type: "info",
            message: "取消删除用户",
          });
        });
    },
    commit() {
      console.log(this.title);
      if (this.title == "添加用户") {
        this.$axios
          .put("http://localhost:8800/user/add", {
            username: this.form.username,
            idcard: this.form.idcard,
            dep: this.form.dep,
            tel: this.form.tel,
            role: this.form.role,
            // leader:this.form.leader.username
          })
          .then((resp) => {
            if (resp.data.code == 200) {
              this.$message({
                message: "用户添加成功！",
                type: "success",
              });
            } else {
              this.$message.error("用户添加失败！");
            }
            this.getUsers();
          });
      } else if (this.title == "修改用户") {
        this.$axios
          .put("http://localhost:8800/user/update", {
            id: this.form.id,
            username: this.form.username,
            idcard: this.form.idcard,
            dep: this.form.dep,
            tel: this.form.tel,
            role: this.form.role,
            // leader:this.form.leader.username
          })
          .then((resp) => {
            if (resp.data.code == 200) {
              this.$message({
                message: "用户修改成功！",
                type: "success",
              });
            } else {
              this.$message.error("用户修改失败！");
            }
            this.getUsers();
          });
      }
      this.dialogFormVisible = false;
    },
  },
};
</script>

<style>
</style>